/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.settlement.service.customercarcontract;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.settlement.dao.customercarcontract.CustomerCarContractDao;
import com.jeesite.modules.settlement.dao.customercarcontract.CustomerCarContractDetailDao;
import com.jeesite.modules.settlement.entity.customercarcontract.CustomerCarContract;
import com.jeesite.modules.settlement.entity.customercarcontract.CustomerCarContractDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户汽车供需合同Service
 * @author LV
 * @version 2021-03-06
 */
@Service
@Transactional(readOnly=true)
public class CustomerCarContractService extends CrudService<CustomerCarContractDao, CustomerCarContract> {
	
	@Autowired
	private CustomerCarContractDetailDao customerCarContractDetailDao;
    @Autowired
    private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param customerCarContract
	 * @return
	 */
	@Override
	public CustomerCarContract get(CustomerCarContract customerCarContract) {
		CustomerCarContract entity = super.get(customerCarContract);
		if (entity != null){
			CustomerCarContractDetail customerCarContractDetail = new CustomerCarContractDetail(entity);
			customerCarContractDetail.setStatus(CustomerCarContractDetail.STATUS_NORMAL);
			entity.setCustomerCarContractDetailList(customerCarContractDetailDao.findList(customerCarContractDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param customerCarContract 查询条件
	 * @param customerCarContract.page 分页对象
	 * @return
	 */
	@Override
	public Page<CustomerCarContract> findPage(CustomerCarContract customerCarContract) {
		return super.findPage(customerCarContract);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param customerCarContract
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(CustomerCarContract customerCarContract) {
        if (customerCarContract.getIsNewRecord()) {
            customerCarContract.setCustomerCarContractCode(codeRuleService.getCode("customercarcontract"));
        }
		super.save(customerCarContract);
        if (customerCarContract.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("customercarcontract");
        }

		// 保存上传附件
		FileUploadUtils.saveFileUpload(customerCarContract.getId(), "customerCarContract_file");
		// 保存 CustomerCarContract子表
		for (CustomerCarContractDetail customerCarContractDetail : customerCarContract.getCustomerCarContractDetailList()){
			if (!CustomerCarContractDetail.STATUS_DELETE.equals(customerCarContractDetail.getStatus())){
				customerCarContractDetail.setCustomerCarContractId(customerCarContract);
				if (customerCarContractDetail.getIsNewRecord()){
					customerCarContractDetailDao.insert(customerCarContractDetail);
				}else{
					customerCarContractDetailDao.update(customerCarContractDetail);
				}
			}else{
				customerCarContractDetailDao.delete(customerCarContractDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param customerCarContract
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(CustomerCarContract customerCarContract) {
		super.updateStatus(customerCarContract);
	}
	
	/**
	 * 删除数据
	 * @param customerCarContract
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(CustomerCarContract customerCarContract) {
		super.delete(customerCarContract);
		CustomerCarContractDetail customerCarContractDetail = new CustomerCarContractDetail();
		customerCarContractDetail.setCustomerCarContractId(customerCarContract);
		customerCarContractDetailDao.deleteByEntity(customerCarContractDetail);
	}
	
}